package kr.couponplay.android.adapter;

import java.util.List;

import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.search.DeleteOnClickListener;
import kr.couponplay.android.domain.DealDao;
import kr.couponplay.android.domain.RecentQuery;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class QueryListAdapter extends ArrayAdapter<RecentQuery> {
	private Context mContext;
	private int mResource;
	private List<RecentQuery> recentQueries;
	private LayoutInflater mInflater;
	private DealDao dao;

	public QueryListAdapter(Context context, int layoutResource, List<RecentQuery> recentQueries, DealDao dao) {
		super(context, layoutResource, recentQueries);
		this.mContext = context;
		this.mResource = layoutResource;
		this.recentQueries = recentQueries;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.dao = dao;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RecentQuery recentQuery = recentQueries.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(mResource, parent, false);
		}

		if (recentQuery != null) {
			TextView title = (TextView) convertView.findViewById(R.id.text_list_row_titleTextView);
			title.setText(recentQuery.getQuery());
			
			TextView deleteBtn = (TextView) convertView.findViewById(R.id.text_list_row_delete);
			DeleteOnClickListener deleteOnClickListener = new DeleteOnClickListener(mContext, this, position, dao);
			deleteBtn.setOnClickListener(deleteOnClickListener);
		}
		return convertView;
	}
	
	public RecentQuery getItem(int position) {
		return recentQueries.get(position);
	}
	
	public int getItemSize() {
		return recentQueries.size();
	}
}
